package com.ywq.app.web.mvc.misc;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.ywq.app.bean.ImageWeb;
import com.ywq.app.biz.ArtMadeBiz;
import com.ywq.app.biz.ArtPicBiz;
import com.ywq.app.biz.ImageBiz;
import com.ywq.app.domain.ArtArtist;
import com.ywq.app.domain.ArtClass;
import com.ywq.app.domain.ArtPic;
import com.ywq.app.web.mvc.test.CommonController;
import com.ywq.common.constant.Constant;
import com.ywq.common.exception.AppBusinessException;
import com.ywq.common.page.PageModel;
@Controller
public class ArtPicMiscController {
	private final static Logger logger = LoggerFactory
	.getLogger(ArtPicMiscController.class);

	@Autowired
	private ArtPicBiz artPicBiz;
	@Autowired
	private ImageBiz imageBiz;
	@Autowired
	private ArtMadeBiz artMadeBiz;
	/**
	 * 艺术画展示列表页面
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/artPic/artPic.htm")
	public String artPic(HttpServletRequest request,
				HttpServletResponse response) throws IOException {
			return "misc/artPic/artPic";
			}
	/**
	 * 艺术画展示列表链接
	 * @param page
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/artPic/artPicGrid.htm")
	public String artPicGrid(Integer page,Integer pagesize, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
	
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			if (page == null) {
				page = 1;
			}
			String type="";
			PageModel pageModelUser = artPicBiz.getArtPicListForMisc(page,type,pagesize);
			pw = response.getWriter();
			pw.print(JSONObject.fromObject(pageModelUser));
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("getArtPicList failure.", e);
			}
		}
		return null;
	}
	/**
	 * 添加艺术画页面
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/artPic/addArtPicWeb.htm")
	public String addArtPicWeb(String classId,String type,HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		request.setAttribute("classId", classId);
		request.setAttribute("type", type);
		try {
				ArtArtist artArtist =artMadeBiz.getArtist(classId);
				if(artArtist!=null){
					request.setAttribute("artName", artArtist.getName());
				}
				if("made".equals(type)){
					request.setAttribute("classType", Constant.PIC_MADE_CLASS);
				}else if("room".equals(type)){
					request.setAttribute("classType", Constant.PIC_ROOM_CLASS);
				}
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		request.setAttribute("have", "no");
		return "misc/artPic/addArtPicWeb";
	}
	@RequestMapping(value = "/misc/artPic/updateArtPicWeb.htm")
	public String updateArtPicWeb(String identifier,HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		try {
			ArtPic artPic=artPicBiz.getArtPic(identifier);
			ArtClass artClass=artPicBiz.getClassById(artPic.getArtClass());
		
			if(artClass!=null){
					artPic.setArtClassName(artClass.getName());
			}
			request.setAttribute("have", "have");	
			request.setAttribute("artPic", artPic);
			if("made".equals(artPic.getType())){
				request.setAttribute("classType", Constant.PIC_MADE_CLASS);
				return "misc/artPic/addArtPicWeb";
			}else if("auction".equals(artPic.getType())){
				request.setAttribute("classType", Constant.PIC_AUCTION_CLASS);
				return "misc/artPic/addArtPicWeb";
			}
			else if("room".equals(artPic.getType())){
				request.setAttribute("classType", Constant.PIC_ROOM_CLASS);
				return "misc/artPic/addArtPicWeb";
			}else if("find".equals(artPic.getType())){
				//尺寸
				ArtClass sizeClass=artPicBiz.getClassById(artPic.getArtSize());
				if(sizeClass!=null){
					artPic.setArtSizeName(sizeClass.getName());
				}
				//材料
				ArtClass materialClass=artPicBiz.getClassById(artPic.getMaterial());
				if(materialClass!=null){
					artPic.setMaterialName(materialClass.getName());
				}
				//用途
				ArtClass useClass=artPicBiz.getClassById(artPic.getArtuse());
				if(useClass!=null){
					artPic.setUseName(useClass.getName());
				}
				//环境
				ArtClass groundClass=artPicBiz.getClassById(artPic.getGround());
				if(groundClass!=null){
					artPic.setGroundName(groundClass.getName());
				}
				
				//范围
				ArtClass rangeClass=artPicBiz.getClassById(artPic.getArtrange());
				if(rangeClass!=null){
					artPic.setRangeName(rangeClass.getName());
				}
				//年代
				ArtClass yearsClass=artPicBiz.getClassById(artPic.getYears());
				if(yearsClass!=null){
					artPic.setYearsName(yearsClass.getName());
				}
				//热门分类
				ArtClass hotClass=artPicBiz.getClassById(artPic.getHot());
				if(hotClass!=null){
					artPic.setHotName(hotClass.getName());
				}
				//专业分类
				ArtClass profClass=artPicBiz.getClassById(artPic.getProf());
				if(profClass!=null){
					artPic.setProfName(profClass.getName());
				}
				//标签
				ArtClass tagClass=artPicBiz.getClassById(artPic.getTag());
				if(tagClass!=null){
					artPic.setTagName(tagClass.getName());
				}
				ArtArtist artArtist =artMadeBiz.getArtist(artPic.getPainter());
				if(artArtist!=null){
					request.setAttribute("artName", artArtist.getName());
					request.setAttribute("artistId", artArtist.getId());
				}
				List<ArtArtist> artist =artMadeBiz.getArtistListForFind();
				request.setAttribute("artist",artist);
				request.setAttribute("classType", Constant.PIC_FIND_CLASS);
				request.setAttribute("profType", Constant.PIC_FIND_PROF);
				request.setAttribute("hotType", Constant.PIC_FIND_HOT);
				return "misc/artFind/addFindArtPicWeb";
			}
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 添加艺术画
	 * @param request
	 * @param response
	 * @param artPic
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/artPic/insertArtPic.htm", method = RequestMethod.POST)
	public String insertArtPic(HttpServletRequest request,
			HttpServletResponse response, ArtPic artPic) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			artPicBiz.insertArtPic(artPic);
			pw = response.getWriter();
			pw.print("success");
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("insertArtPic failure.", e);
			}
		}
		return null;
	}
	/**
	 * 上传图片
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/artPic/uploadImg.htm")
	public String uploadImg(HttpServletRequest request,HttpServletResponse response) throws IOException {
		//System.out.print("111111111");
		response.setContentType("text/html; charset=UTF-8"); 
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter pw = null; 
		try {
			String imageDir=getRootPath();
			ImageWeb imageWeb = imageBiz.uploadOneImg(request, Constant.IMAGE_PATH_ART,imageDir);
			pw = response.getWriter();
			pw.print(JSONObject.fromObject(imageWeb));
			//System.out.print(imageWeb);
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("Add user failure.", e);
			}
		}
		return null;
	} 
	
	/**
	 * 分类页面
	 */
	//展览馆
	@RequestMapping(value = "/misc/artPic/artRoomClass.htm")
	public String artRoomClass(HttpServletRequest request,HttpServletResponse response) throws IOException {
		request.setAttribute("type", Constant.PIC_ROOM_CLASS);
		return "misc/artPic/artClass";
	}
	//定制
	@RequestMapping(value = "/misc/artPic/artMadeClass.htm")
	public String artMadeClass(HttpServletRequest request,HttpServletResponse response) throws IOException {
		request.setAttribute("type", Constant.PIC_MADE_CLASS);
		return "misc/artPic/artClass";
	}
	//拍卖
	@RequestMapping(value = "/misc/artPic/artAuctionClass.htm")
	public String artAuctionClass(HttpServletRequest request,HttpServletResponse response) throws IOException {
		request.setAttribute("type", Constant.PIC_AUCTION_CLASS);
		return "misc/artPic/artClass";
	}
	//发现
	@RequestMapping(value = "/misc/artPic/artFindClass.htm")
	public String artFindClass(HttpServletRequest request,HttpServletResponse response) throws IOException {
		request.setAttribute("type", Constant.PIC_FIND_CLASS);
		return "misc/artPic/artClass";
	}
	//热门分类
	@RequestMapping(value = "/misc/artPic/artHotClass.htm")
	public String artHotClass(HttpServletRequest request,HttpServletResponse response) throws IOException {
		request.setAttribute("type", Constant.PIC_FIND_HOT);
		return "misc/artPic/artClass";
	}
	//专业分类
	@RequestMapping(value = "/misc/artPic/artProfClass.htm")
	public String artProfClass(HttpServletRequest request,HttpServletResponse response) throws IOException {
		request.setAttribute("type", Constant.PIC_FIND_PROF);
		return "misc/artPic/artClass";
	}
	//生成分类树
    @RequestMapping(value = "/misc/artPic/classTree.htm")
    public String classTree(String type,HttpServletRequest request, HttpServletResponse response) {
        try {
            List<ArtClass> classList = artPicBiz.getChildById("0",type);
            String treeJson = ArtClass.getClassTree(classList);
            request.setAttribute("treeJson", treeJson);
        } catch (AppBusinessException e) {
            e.printStackTrace();
        }
        return "misc/artPic/classTree";
    }
    // 添加分类
    @RequestMapping(value = "/misc/artPic/cmsSaveClass.htm")
    public String cmsSaveClass(ArtClass artClass, HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html; charset=UTF-8");
            PrintWriter out = response.getWriter();
            artPicBiz.addArtClass(artClass);
            out.print("success");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    // 打开修改分类页面
    @RequestMapping(value = "/misc/cms/toeditchannel.htm")
    public String toeditchannel(HttpServletRequest request, HttpServletResponse response, String id) {
        try {
        	ArtClass artClass = artPicBiz.getClassById(id);
            request.setAttribute("artClass", artClass);
        } catch (AppBusinessException e) {
            logger.error("打开修改频道页面 错误", e);
            e.printStackTrace();
        }
        return "misc/artPic/editClass";
    }

    // 修改分类
    @RequestMapping(value = "/misc/cms/editchannel.htm")
    public String editchannel(HttpServletRequest request, HttpServletResponse response, ArtClass artClass) {
        try {
            response.setContentType("text/html; charset=UTF-8");
            artPicBiz.updateClassById(artClass);
            response.getWriter().print("success");
        } catch (Exception e) {
            logger.error("打开修改频道页面 错误", e);
            e.printStackTrace();
        }
        return null;
    }
    public static String getRootPath(){   
        //因为类名为"CommonController"，因此" CommonController.class"一定能找到   
        String result = CommonController.class.getResource("CommonController.class").toString();   
        int index = result.indexOf("WEB-INF");   
        if(index == -1){   
            index = result.indexOf("bin");   
        }   
        result = result.substring(0,index);   
        if(result.startsWith("jar")){   
            // 当class文件在jar文件中时，返回"jar:file:/F:/ ..."样的路径    
            result = result.substring(10);   
        }else if(result.startsWith("file")){   
            // 当class文件在class文件中时，返回"file:/F:/ ..."样的路径    
            result = result.substring(6);   
        }   
        if(result.endsWith("/"))result = result.substring(0,result.length()-1);//不包含最后的"/"   
        return result;   
    } 
    //删除作品
    @RequestMapping(value = "/misc/artPic/deletePic.htm")
    public String deletePic(String identifier,HttpServletRequest request, HttpServletResponse response, ArtClass artClass) {
        try {
            response.setContentType("text/html; charset=UTF-8");
            artPicBiz.deletePic(identifier);
            response.getWriter().print("success");
        } catch (Exception e) {
            logger.error("打开修改频道页面 错误", e);
            e.printStackTrace();
        }
        return null;
    }
    //更新作品
    @RequestMapping(value = "/misc/artPic/updatePic.htm")
    public String updatePic(ArtPic artPic,HttpServletRequest request, HttpServletResponse response, ArtClass artClass) {
        try {
            response.setContentType("text/html; charset=UTF-8");
            artPicBiz.updatePic(artPic);
            response.getWriter().print("success");
        } catch (Exception e) {
            logger.error("打开修改频道页面 错误", e);
            e.printStackTrace();
        }
        return null;
    }
    //精选
    @RequestMapping(value = "/misc/artPic/choice.htm")
    public String choice(ArtPic artPic,HttpServletRequest request, HttpServletResponse response, ArtClass artClass) {
        try {
            response.setContentType("text/html; charset=UTF-8");
            artPic.setChoice(true);
            artPicBiz.updatePic(artPic);
            response.getWriter().print("success");
        } catch (Exception e) {
            logger.error("打开修改频道页面 错误", e);
            e.printStackTrace();
        }
        return null;
    }
  //取消精选
    @RequestMapping(value = "/misc/artPic/noChoice.htm")
    public String noChoice(ArtPic artPic,HttpServletRequest request, HttpServletResponse response, ArtClass artClass) {
        try {
            response.setContentType("text/html; charset=UTF-8");
            artPic.setChoice(false);
            artPicBiz.updatePic(artPic);
            response.getWriter().print("success");
        } catch (Exception e) {
            logger.error("打开修改频道页面 错误", e);
            e.printStackTrace();
        }
        return null;
    }
    // { "text": "材料管理","url":"/misc/artPic/picMaterial.htm"},
    ///{ "text": "年代管理","url":"/misc/artPic/picYears.htm"},
    //{ "text": "适用环境","url":"/misc/artPic/picGround.htm"},
    //{ "text": "适用用途","url":"/misc/artPic/picUse.htm"}
    //尺寸
    @RequestMapping(value = "/misc/artPic/picSize.htm")
	public String picSize(HttpServletRequest request,HttpServletResponse response) throws IOException {
    	try {
			List<ArtClass> classList=artPicBiz.getChildById("0", Constant.PIC_SIZE_CLASS);
			request.setAttribute("classList", classList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
    	request.setAttribute("parentVal", 0);
    	request.setAttribute("type", Constant.PIC_SIZE_CLASS);
		return "misc/artPic/artTypeClass";
	}
    //材料
    @RequestMapping(value = "/misc/artPic/picMaterial.htm")
	public String picMaterial(HttpServletRequest request,HttpServletResponse response) throws IOException {
    	try {
			List<ArtClass> classList=artPicBiz.getChildById("0", Constant.PIC_MATERIAL_CLASS);
			request.setAttribute("classList", classList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
    	request.setAttribute("parentVal", "0");
    	request.setAttribute("type", Constant.PIC_MATERIAL_CLASS);
		return "misc/artPic/artTypeClass";
	}
    //年代
    @RequestMapping(value = "/misc/artPic/picYears.htm")
	public String picYears(HttpServletRequest request,HttpServletResponse response) throws IOException {
    	try {
			List<ArtClass> classList=artPicBiz.getChildById("0", Constant.PIC_YEARS_CLASS);
			request.setAttribute("classList", classList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
    	request.setAttribute("parentVal", 0);
    	request.setAttribute("type", Constant.PIC_YEARS_CLASS);
		return "misc/artPic/artTypeClass";
	}
    //环境
    @RequestMapping(value = "/misc/artPic/picGround")
	public String picGround(HttpServletRequest request,HttpServletResponse response) throws IOException {
    	try {
			List<ArtClass> classList=artPicBiz.getChildById("0", Constant.PIC_GROUND_CLASS);
			request.setAttribute("classList", classList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
    	request.setAttribute("parentVal", 0);
    	request.setAttribute("type", Constant.PIC_GROUND_CLASS);
		return "misc/artPic/artTypeClass";
	}
    //用途
    @RequestMapping(value = "/misc/artPic/picUse")
	public String picUse(HttpServletRequest request,HttpServletResponse response) throws IOException {
    	try {
			List<ArtClass> classList=artPicBiz.getChildById("0", Constant.PIC_USE_CLASS);
			request.setAttribute("classList", classList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
    	request.setAttribute("parentVal", 0);
    	request.setAttribute("type", Constant.PIC_USE_CLASS);
		return "misc/artPic/artTypeClass";
	}
    //价格
    @RequestMapping(value = "/misc/artPic/picPrice")
	public String picPrice(HttpServletRequest request,HttpServletResponse response) throws IOException {
    	try {
			List<ArtClass> classList=artPicBiz.getChildById("0", Constant.PIC_PRICE_CLASS);
			request.setAttribute("classList", classList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
    	request.setAttribute("parentVal", 0);
    	request.setAttribute("type", Constant.PIC_PRICE_CLASS);
		return "misc/artPic/artTypeClass";
	}
    //标签
    @RequestMapping(value = "/misc/artPic/picTag.htm")
	public String picTag(HttpServletRequest request,HttpServletResponse response) throws IOException {
    	try {
			List<ArtClass> classList=artPicBiz.getChildById("0", Constant.PIC_TAG_CLASS);
			request.setAttribute("classList", classList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
    	request.setAttribute("parentVal", 0);
    	request.setAttribute("type", Constant.PIC_TAG_CLASS);
		return "misc/artPic/artTypeClass";
	}
   /* //生成分类树
    @RequestMapping(value = "/misc/artPic/classTree.htm")
    public String classTypeTree(String type,HttpServletRequest request, HttpServletResponse response) {
        try {
            List<ArtClass> classList = artPicBiz.getChildById("0",type);
            String treeJson = ArtClass.getClassTree(classList);
            request.setAttribute("treeJson", treeJson);
        } catch (AppBusinessException e) {
            e.printStackTrace();
        }
        return "misc/artPic/classTree";
    }*/
    //作废分类
    @RequestMapping(value = "/misc/artPic/deleteClassList.htm")
    public String deleteClassList(ArtClass artClass,HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html; charset=UTF-8");
            artClass.setEnabled(false);
            artPicBiz.updateClassById(artClass);
            response.getWriter().print("success");
        } catch (Exception e) {
            logger.error("作废 错误", e);
            e.printStackTrace();
        }
        return null;
    }
    //按照媒介
    @RequestMapping(value = "/misc/member/media.htm")
	public String media(HttpServletRequest request,HttpServletResponse response) throws IOException {
    	try {
			List<ArtClass> classList=artPicBiz.getChildById("0", Constant.PIC_MEDIA_CLASS);
			request.setAttribute("classList", classList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
    	request.setAttribute("parentVal", "0");
    	request.setAttribute("type", Constant.PIC_MEDIA_CLASS);
		return "misc/artPic/hobby";
	}
  //按照题材
    @RequestMapping(value = "/misc/member/theme.htm")
	public String theme(HttpServletRequest request,HttpServletResponse response) throws IOException {
    	try {
			List<ArtClass> classList=artPicBiz.getChildById("0", Constant.PIC_THEME_CLASS);
			request.setAttribute("classList", classList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
    	request.setAttribute("parentVal", "0");
    	request.setAttribute("type", Constant.PIC_THEME_CLASS);
		return "misc/artPic/hobby";
	}
  //按照风格/运动
    @RequestMapping(value = "/misc/member/sport.htm")
	public String sport(HttpServletRequest request,HttpServletResponse response) throws IOException {
    	try {
			List<ArtClass> classList=artPicBiz.getChildById("0", Constant.PIC_SPORT_CLASS);
			request.setAttribute("classList", classList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
    	request.setAttribute("parentVal", "0");
    	request.setAttribute("type", Constant.PIC_SPORT_CLASS);
		return "misc/artPic/hobby";
	}
  //按照部门
    @RequestMapping(value = "/misc/member/department.htm")
	public String department(HttpServletRequest request,HttpServletResponse response) throws IOException {
    	try {
			List<ArtClass> classList=artPicBiz.getChildById("0", Constant.PIC_DEPARTMENT_CLASS);
			request.setAttribute("classList", classList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
    	request.setAttribute("parentVal", "0");
    	request.setAttribute("type", Constant.PIC_DEPARTMENT_CLASS);
		return "misc/artPic/hobby";
	}
    //富文本上传图片
    @RequestMapping(value = "/misc/xheditor/uploadImg.htm")
	public String xheditoruploadImg(HttpServletRequest request,HttpServletResponse response) throws IOException {
		//System.out.print("111111111");
		response.setContentType("text/html; charset=UTF-8"); 
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter pw = null; 
		try {
			String imageDir=getRootPath();
			ImageWeb imageWeb = imageBiz.uploadOneImg(request, Constant.IMAGE_PATH_ART,imageDir);
			pw = response.getWriter();
			String outputJson = "{'err':'','msg':"+JSONObject.fromObject(imageWeb)+"}";
			pw = response.getWriter();
			pw.print(JSONObject.fromObject(outputJson)); 
			//System.out.print(imageWeb);
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("Add user failure.", e);
			}
		}
		return null;
	} 
  
}
